package com.niuquan.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.niuquan.activitie.R;
import com.niuquan.modal.ContactsItem;
import com.niuquan.uicomponent.CircularImage;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;

/**
 * Created by DELL on 2015/5/23.
 */
public class ContactListAdapter extends BaseAdapter {
    public ArrayList<ContactsItem> m_items;
    private LayoutInflater inflater=null;
    private Context context;
    public ContactListAdapter() {
    }
    public ContactListAdapter(Context context,ArrayList<ContactsItem> items)
    {
        this.context = context;
        m_items = items;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = inflater.inflate(R.layout.layout_contactsitem, null);
        }
        CircularImage headpic = (CircularImage) convertView.findViewById(R.id.allcontactsheadpic);
        TextView username = (TextView) convertView.findViewById(R.id.allcontacts_username);
        TextView tag = (TextView) convertView.findViewById(R.id.allcontacts_tag);

        username.setText(m_items.get(position).getNickname());
        tag.setText(m_items.get(position).getM_tag());
        //      Uri headpicpath = Uri.parse(m_items.get(position).getM_headpicpath());
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(m_items.get(position).getM_headpicpath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Bitmap bitmap  = BitmapFactory.decodeStream(fis);
        headpic.setImageBitmap(bitmap);
        return convertView;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public ContactsItem getItem(int position) {
        return m_items.get(position);
    }

    @Override
    public int getCount() {
        return m_items.size();
    }
}
